var app = getApp()
var auth = require('../../lib/auth/login')
var tools = require('../../lib/system/tools')
var pages = require('../../lib/common/pageConstant')
var commonFunc = require('../../lib/system/common')
var commonConstant = require('../../lib/common/constant')
var businessStorage = require('../../lib/common/storage')

Page({
    data: {
        orderId: 'a53981ef75ee4554bf8264799bbda171',
        orderDetail: {
            "changed_by": "王岩",
            "changed_on": "2017-11-02T15:05:26",
            "checkout_datetime": "2017-11-03T23:03:29",
            "created_by": "王岩",
            "created_on": "2017-11-02T15:05:26",
            "enable": true,
            "id": "1b36e0b648ec45a396f9c538038025ad",
            "payment_datetime": "2017-11-03T23:03:29",
            "payment_method": "线上支付",
            "qr_code": null,
            "quantity": 1,
            "settlement_price": 58,
            "snapshot": {
                "photo": '/static/uploads/87219c4c-b994-11e7-9d11-000c2951e0ca_sep_3.jpg',
                "description": '',
                "price": "38.00",
                "weight_g": 5,
                "order_price": "38.00",
                "deposit_price": "1.00",
                "express_price": "0.00",
                "discount_price": "0.00",
                "settlement_price": "39.00"
            },
            "status": 1,
            "user_coupon_id": null,
            "weapp_logistics_location": "测试物流点1",
            "weapp_logistics_location_id": "f70b1dff-b992-11e7-9d11-000c2951e0ca",
            "weapp_product_distribution": "自提",
            "weapp_product_distribution_id": "1201688145064b1c9130630cbf312911",
            "weapp_user": "测试小程序用户",
            "weapp_user_id": "3027de32fc4141dbbefd3ad24a7d7ec4"
        },
        commonConstant: commonConstant
    },
    onLoad: function (query) {
        var currentOrderId = commonFunc.getParam(query, 'order_id', '00d1bba8d8144735b5c9b6d50ec42ee8')
        var that = this
        console.log('order:' + currentOrderId)
        this.setData({
            orderId: currentOrderId
        })
        tools.requestWithAuth({
            url: 'user_order',
            data: {
                order_id: currentOrderId
            },
            feedback: {
                success: data => {
                    var order = data[0]
                    console.log(order)
                    order['snapshot'] = JSON.parse(order['snapshot'])
                    that.setData({
                        orderDetail: order
                    })
                }
            }
        })
    },
    confirmOrder: function (event) {
        var that = this
        console.log('order:' + that.data.orderId)
        tools.requestWithAuth({
            url: 'confirm_order',
            method: 'PUT',
            data: {
                order_id: that.data.orderId
            },
            feedback: {
                success: res => {
                    console.log(res)
                    wx.showToast({
                        title: '确认收货成功',
                        success: function () {
                            wx.switchTab({
                                url: pages.homePage
                            })
                        }
                    })
                }
            }
        })        
    }
})